// 首页逻辑
const essayApi = require('../../api/essayApi');
const app = getApp();

Page({
  data: {
    points: 0,
    historyList: [],
    loading: false
  },

  onLoad: function (options) {
    // 页面初始化
  },
  
  onShow: function () {
    // 每次显示页面时更新积分
    this.setData({
      points: app.globalData.points || 0
    });
    
    // 获取批改历史
    this.getHistory();
  },
  
  // 获取批改历史
  getHistory: function () {
    if (this.data.loading) return;
    
    this.setData({ loading: true });
    
    essayApi.getCorrectionHistory(1, 2)  // 只获取最近2条
      .then(result => {
        this.setData({
          historyList: result.list || []
        });
      })
      .catch(err => {
        console.error('获取批改历史失败', err);
      })
      .finally(() => {
        this.setData({ loading: false });
        
        // 停止下拉刷新
        wx.stopPullDownRefresh();
      });
  },
  
  // 跳转到批改详情
  goToDetail: function (e) {
    const id = e.currentTarget.dataset.id;
    
    wx.navigateTo({
      url: `/pages/correction/correction?id=${id}`
    });
  },
  
  // 下拉刷新
  onPullDownRefresh: function () {
    this.getHistory();
  }
}); 